package com.itqf.Service.Ordertopic.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.Dao.OrderMapper;
import com.itqf.Dao.OrdertopicMapper;
import com.itqf.Service.Ordertopic.OrderTopicService;
import com.itqf.entity.Order;
import com.itqf.entity.Ordertopic;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderTopicServiceImpl implements OrderTopicService
{
    @Resource
    private OrdertopicMapper ordertopicMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     *订单的查询 以及模糊分页的显示
     * */
    @Override
    public PageInfo FindByLike(int pageindex, Integer pagesize, int topicState, String  userid) {
        PageHelper.startPage(pageindex,pagesize);

        Map map=new HashMap();

        map.put("userid",userid);
        map.put("topicState",topicState);

        List<Order> orderList= ordertopicMapper.findOrderByUserId(map);

        PageInfo pageInfo=new PageInfo(orderList);
        return pageInfo;
    }
  /**
  * 订单的删除
  * */
    @Override
    @Transactional
    public boolean deleteByTopicId(int ordertopicid) {

        boolean delete=false;
        Map map=new HashMap();
        map.put("orderid",ordertopicid);


       int  i= ordertopicMapper.updatebyOrderTopicidWithState(ordertopicid);


//        int i = orderMapper.deleteOrder(map);

//        System.out.println(5/0);
//        if (i>0)
//        {
//            Map map1=new HashMap();
//            map1.put("ordertopicid",ordertopicid);
//          int i1 = ordertopicMapper.deleteOrdertopic(map1);
          if (i>0)
          {
              delete=true;
          }

        return delete;
    }

    @Override
    public Ordertopic findordertopic(String orderid) {
        Ordertopic ordertopic = ordertopicMapper.selectByPrimaryKey(orderid);
        return ordertopic;
    }

    @Override
    public void insertordertopic(Ordertopic ordertopic) {
        ordertopicMapper.insert(ordertopic);
    }

    @Override
    public void updateordertopic(String orderid1, int topicInt, double v) {
        Map map=new HashMap();
        map.put("orderid",orderid1);
        map.put("topicInt",topicInt);
        map.put("totalprice",v);
        ordertopicMapper.updateordertopic(map);
    }

    @Override
    public void updateordertopicbyaddress(String orderid1, int shippingid) {
        Map map=new HashMap();
        map.put("orderid1",orderid1);
        map.put("shippingid",shippingid);
        ordertopicMapper.updateordertopicbyaddress(map);
    }

    @Override
    @Transactional
    public int updatebyordertopicid(String ordertopicid,String userid) {
        int a=ordertopicMapper.updatebyordertopicid(ordertopicid);
        Order order=new Order();
        order.setOrderid(ordertopicid);
        order.setUserid(userid);
        int b =orderMapper.insert(order);
        int paycode=0;
        if(a>0&&b>0)
        {
            paycode=200;
        }
        else {
            paycode=400;
        }
        return paycode;
    }

}
